<?php


namespace tools\es;


use think\Paginator;

class EsPage
{
    public static function paginate($results, $listRows, $simple = false): Paginator
    {
        if (is_int($simple)) {
            $total = $simple;
            $simple = false;
        }

        $defaultConfig = [
            'query' => [], // url额外参数
            'fragment' => '', // url锚点
            'var_page' => 'page', // 分页变量
            'list_rows' => 15, // 每页数量
        ];

        if (is_array($listRows)) {
            $config   = array_merge($defaultConfig, $listRows);
            $listRows = intval($config['list_rows']);
        } else {
            $config   = $defaultConfig;
            $listRows = intval($listRows ?: $config['list_rows']);
        }

        $page = isset($config['page']) ? (int) $config['page'] : Paginator::getCurrentPage($config['var_page']);
        $page = $page < 1 ? 1 : $page;
        $config['path'] = $config['path'] ?? Paginator::getCurrentPath();
        return Paginator::make($results, $listRows, $page, $total, $simple, $config);
    }
}